package com.websockettest.controller;

import com.websockettest.sse.SseClientUtil;
import com.websockettest.websocket.WebSocketSessionUtils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MessageController {
    private ExecutorService executorService = Executors.newCachedThreadPool();

    @ResponseBody
    @RequestMapping(value = "/sendMsg")
    public String sendMsg(String msg, String userId) {
        executorService.submit(() -> {
            try {
                // 延迟2s后推送消息
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // webSocket推送消息
            WebSocketSessionUtils.sendMessage(userId, msg);
            // SSE推送消息
            SseClientUtil.sendMessage(userId, userId, msg);
        });
        return "send " + msg + " success";

    }
}
